let md5 = require('md5')

//生成随机字符串
function getRnd32() {
    let str = 'QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm1234567890'
    let res = ''
    for (let i=0;i<32;i++){
        let num = Math.floor(Math.random()*str.length)
        res+=str[num]
    }
    return res
}
//生成时间戳
function getTimeStamp() {
    return Math.round(new Date().getTime()/1000)
}
//生成商户订单号
function getTradeNo(){
    let date= new Date()
    let arr = [
        date.getFullYear(),
        (date.getMonth())+1,
        date.getDate(),
        date.getHours(),
        date.getMinutes(),
        date.getSeconds(),
        date.getMilliseconds(),
        Math.floor(Math.random()*100000)
    ]
    return arr.join('')
}
// 获取本机电脑IP
function getIPAdress() {
    let interfaces = require('os').networkInterfaces();
    for (var devName in interfaces) {
        var iface = interfaces[devName];
        for (var i = 0; i < iface.length; i++) {
            let alias = iface[i];
            if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal) {
                // console.log(alias.address);

                return alias.address
            }
        }
    }
}
//生成微信签名
function getSign(payobj,wx_key){
    let arr= [];
    for (const key in payobj){
        arr.push(key + '=' + payobj[key])
    }
    //
    arr = arr.sort(function (a,b){
        return a.localeCompare(b)
    })
    arr.push("key=" +wx_key);
    arr = arr.join('&')
    arr = md5(arr)
    return arr
}

module.exports = {
    getRnd32,
    getTimeStamp,
    getTradeNo,
    getIPAdress,
    getSign
}